import request from '@/utils/request'
import * as mock from './mock/project'
import * as mockRecords from './mock/participantRecords'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 获取项目列表
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getProjectList(params) {
  if (isMock) {
    return Promise.resolve(mock.getProjectList(params))
  }
  return request({
    url: '/project/list',
    method: 'get',
    params
  })
}

/**
 * 获取项目详情
 * @param {string|number} id 项目ID
 * @returns {Promise}
 */
export function getProjectDetail(id) {
  if (isMock) {
    return Promise.resolve(mock.getProjectDetail(id))
  }
  return request({
    url: `/project/detail/${id}`,
    method: 'get'
  })
}

/**
 * 参与项目
 * @param {Object} data 参与数据
 * @returns {Promise}
 */
export function joinProject(data) {
  if (isMock) {
    return Promise.resolve(mock.joinProject(data))
  }
  return request({
    url: '/project/join',
    method: 'post',
    data
  })
}

/**
 * 获取项目参与记录
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getParticipantRecords(params) {
  if (isMock) {
    return Promise.resolve(mockRecords.getParticipantRecords(params))
  }
  return request({
    url: '/project/participants',
    method: 'get',
    params
  })
}
